package cn.edu.neusoft.zengweidong707.fragmentdemo;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
        FavorFragment.OnFragmentInteractionListener, HomeFragment.OnFragmentInteractionListener,
        CollectFragment.OnFragmentInteractionListener, FocusFragment.OnFragmentInteractionListener{
    LinearLayout tab_favor;
    LinearLayout tab_home;
    LinearLayout tab_collect;
    LinearLayout tab_focus;

    ImageView img_favor;
    ImageView img_home;
    ImageView img_collect;
    ImageView img_focus;

    Fragment fragment_home;
    Fragment fragment_favor;
    Fragment fragment_collect;
    Fragment fragment_focus;

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(0);
    }

    private void initEvent() {
        tab_favor.setOnClickListener(this);

        tab_home.setOnClickListener(this);

        tab_collect.setOnClickListener(this);

        tab_focus.setOnClickListener(this);
    }

    private void initView() {
        tab_favor = (LinearLayout)findViewById(R.id.tab_favor);
        tab_home = (LinearLayout)findViewById(R.id.tab_home);
        tab_collect = (LinearLayout)findViewById(R.id.tab_collect);
        tab_focus = (LinearLayout)findViewById(R.id.tab_focus);
        img_favor = (ImageView)findViewById(R.id.img_favor);
        img_home = (ImageView)findViewById(R.id.img_home);
        img_collect = (ImageView)findViewById(R.id.img_collect);
        img_focus = (ImageView)findViewById(R.id.img_focus);

    }

    @Override
    public void onClick(View view) {
        //所有的图片设成默认
        resetImage();
        switch (view.getId()) {
            case R.id.tab_favor:
                setSelect(0);
                break;
            case R.id.tab_home:
                setSelect(1);
                break;
            case R.id.tab_collect:
                setSelect(2);
                break;
            case R.id.tab_focus:
                setSelect(3);
                break;
            default:break;
        }
    }

    //所有的图片设成默认
    private void resetImage() {
        img_favor.setImageResource(R.drawable.ic_favor_nomal);
        img_home.setImageResource(R.drawable.ic_shouye_normal);
        img_collect.setImageResource(R.drawable.ic_cart_normal);
        img_focus.setImageResource(R.drawable.ic_search_normal);
    }

    //选择哪组，哪个Fragment就显现出来，并且图片设成高亮的
    private void setSelect(int i) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i) {
            case 0:
                if(fragment_favor == null) {
                    fragment_favor = new FavorFragment();
                    fragmentTransaction.add(R.id.content, fragment_favor);
                } else {
                    fragmentTransaction.show(fragment_favor);
                }
                img_favor.setImageResource(R.drawable.ic_favor_press);
                break;
            case 1:
                if(fragment_home == null) {
                    fragment_home = new HomeFragment();
                    fragmentTransaction.add(R.id.content, fragment_home);
                } else {
                    fragmentTransaction.show(fragment_home);
                }
                img_home.setImageResource(R.drawable.ic_shouye_press);
                break;
            case 2:
                if(fragment_collect == null) {
                    fragment_collect = new CollectFragment();
                    fragmentTransaction.add(R.id.content, fragment_collect);
                } else {
                    fragmentTransaction.show(fragment_collect);
                }
                img_collect.setImageResource(R.drawable.ic_cart_press);
                break;
            case 3:
                if(fragment_focus == null) {
                    fragment_focus = new FocusFragment();
                    fragmentTransaction.add(R.id.content, fragment_focus);
                } else {
                    fragmentTransaction.show(fragment_focus);
                }
                img_focus.setImageResource(R.drawable.ic_search_press);
                break;
            default:break;
        }
        fragmentTransaction.commit();//提交
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        String s = uri.toString();
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if(fragment_favor != null) {
            fragmentTransaction.hide(fragment_favor);
        }
        if(fragment_home != null) {
            fragmentTransaction.hide(fragment_home);
        }
        if(fragment_collect != null) {
            fragmentTransaction.hide(fragment_collect);
        }
        if(fragment_focus != null) {
            fragmentTransaction.hide(fragment_focus);
        }
    }
}
